import { defineStore } from "pinia";
import { ref, reactive, computed } from "vue";
import LocalStorage from "@/utils/localStorage";
import SessionStorage from "@/utils/sessionStorage";
import { ArrayFormatVueRouter, FormatTreeList } from "@/utils/format";
// 引入Vue3 路由
import { useRouter } from "vue-router";
// 引入env
const ENV = import.meta.env;

export const useRouterStore = defineStore("routerStore", () => {

    const Router = useRouter();

    const RoutesList = ref([])

    const BreadcrumbList = ref([])
    let {
        VITE_ROUTE_LOGIN_PATH,
        VITE_ROUTE_HOME_PATH,
        VITE_ROUTER_STORAGE,
        VITE_ROUTER_STORAGE_TYPE,
        VITE_ROUTER_STORAGE_KEY
    } = ENV
    function ClearRoutes() {
        RoutesList.value = []
    }
    function SetRoutes(data) {
        if (VITE_ROUTER_STORAGE == 'true') {
            if (VITE_ROUTER_STORAGE_TYPE == 'localStorage') {
                LocalStorage.Set(VITE_ROUTER_STORAGE_KEY, data)
            } else if (VITE_ROUTER_STORAGE_TYPE == "sessionStorage") {
                SessionStorage.Set(VITE_ROUTER_STORAGE_KEY, data)
            }
        }
        RoutesList.value = data
    }
    // 隐藏所有子菜单
    function HideAllChildrenren() {
        const k = (list) => {
            list.forEach((menuItem) => {
                menuItem.meta.childrenDevelop = false;
                if (menuItem.children) {
                    k(menuItem.children);
                }
            })
        }
        k(RoutesList.value)
    }

    // 获取面包屑
    function SetBreadcrumbList(list) {
        BreadcrumbList.value = list
    }

    // 跳转到登录页面
    function ToLogin(replace = false) {
        if (replace) {
            Router.replace({ path: VITE_ROUTE_LOGIN_PATH })
        } else {
            Router.push({ path: VITE_ROUTE_LOGIN_PATH })
        }
    }
    // 跳转到首页

    function ToHome(replace = false) {
        if (replace) {
            return Router.replace({ path: VITE_ROUTE_HOME_PATH })
        } else {
            return Router.push({ path: VITE_ROUTE_HOME_PATH })
        }
    }
    return {
        BreadcrumbList,
        SetBreadcrumbList,
        Navigator,
        ToLogin, ToHome,
        RoutesList, SetRoutes,
        ClearRoutes,
        HideAllChildrenren
    }
})